<?php
//自动生成,请根据需要修改
namespace app\service\controller;
use app\common\controller\Base;
use bar\baz\source_with_namespace;
use think\facade\Request;
use think\Db;

/**
* @title 客服服务接口
* @description 客服服务说明
* @group 客服
*/
class ServiceServer extends Base{

    public $product_urls=array(
        'wholesale'=>"http://qianxun.zanboon.com/wholesale/good/get_good_info",//拼团产品
        'wholesale_sku'=>"http://qianxun.zanboon.com/wholesale/good/sku_info",//拼团产品sku

        'goods'=>"http://qianxun.zanboon.com/goods/good/get_good_info",//普通商品
        'goods_sku'=>"http://qianxun.zanboon.com/goods/good_sku/info",//普通sku

        'bargain'=>"http://qianxun.zanboon.com/bargain/good/get_good_info",//砍价
        'bargain_sku'=>"http://qianxun.zanboon.com/bargain/good/sku_info",//砍价sku

        'spike'=>"http://qianxun.zanboon.com/spike/spike_goods/get_good_info",//秒杀
        'spike_sku'=>"http://qianxun.zanboon.com/spike/spike_good_sku/info",//秒杀sku
    );

    public $order_url=array(
        'order'=>"http://qianxun.zanboon.com/order/order/line_item",//订单详情
    );

    public $token="os2rftxaXgM0vWL1QzOH";

	/**
	* @title 获取咨询窗口
	* @description 获取咨询窗口,创建时帐号和商家id必须
	* @author 开发者（梁敏）
	* @url /service/service_server/get_chat
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:account_id type:Int require:1 default:0 other: desc:帐号id
    * @param name:account_type type:String require:1 default:0 other: desc:帐号类型
    * @param name:merchant_id type:Int require:0 default:0 other: desc:商家id
    * @param name:merchant_type type:String require:0 default:0 other: desc:商家类型
	* @param name:chat_id type:Int require:0 default:0 other: desc:窗口id
        * @param name:user_identity type:Varchar require:0 default:0 other: desc:用户身份

     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
	public function get_chat (){
		//模块 - 输入数据
		$input=input();
		unset($input['token']);
        unset($input['sys_sign']);
		//模块 - 验证器
        $validate = new \think\Validate;
        $validate->rule([
            'account_id'  => 'number',
            'account_type'=>'require',
            'merchant_id'  => 'number',
            'merchant_type'=>'require',
            'chat_id'  => 'number',
            'user_identity'=>'require',
        ]);
        if (!$validate->check($input)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }
//        if(isset($input['chat_id'])){
//            $where['chat_id']=$input['chat_id'];
//        }

//        if(isset($input['merchant_id'])){
//            $where['merchant_id']=$input['merchant_id'];
//        }
        $temp=[
            'user_id'=>$input['account_id'],
            'merchant_id'=>$input['merchant_id'],
            'merchant_type'=>$input['merchant_type'],
            'user_identity'=>$input['user_identity'],
            'user_type'=>$input['account_type']
        ];
		//获取对话框
		$service_chat = Db::name('service_chat')->where($temp)->find();

        if(!$service_chat){//如果还没有，要创建！

            //$service_user=Db::name('service_user')->field('service_name,head_img')->where('chat_id',$input['chat_id'])->find();
            //Db::name('service_chat')->where('chat_id',$input['chat_id'])->update(array('service_title'=>$service_user['service_name'],'logo'=>$service_user['head_img']));

            $account = get_user($input['account_id'],$input['account_type']);
            //获取商城管理员
            $params['token']=$this->token;
            $params['merchant_id']=$input['merchant_id'];
//             halt($params);
            $admins=get_member_list($params,$input['merchant_type']);
//             halt($admins);
            if (!$admins) {
                $result['status'] = false;
                $result['msg'] = '该商家没有客服';
                $result['params'] = $params;
                return $this->errorJson($result);
            } else {
                $admin_account_ids=[];//客服的id
                $member_account_ids=[];//窗口聊天人员，类型_id
//                $admin_accounts_send=[];//要发送窗口发起消息
                foreach ($admins as $admin){
                    $member_account_id=$admin['account_type']."_".$admin['account_id'];
                    $member_account_ids[]=$member_account_id;
                    $admin_account_ids[]=$admin['account_id'];
                    $admin_accounts_send[]=$admin['account_type']."_".$admin['account_id'];
                }
//                halt($admin_account_ids);
                if($account){
                    $member_account_id=$account['account_type']."_".$account['account_id'];
                }else{
                    $msg['msg']="用户不存在";
                    return $this->errorJson($msg);
                }

                $member_account_ids[]=$member_account_id;

                //把用户也添加到聊天窗口
                $member_account_ids[]=$input['account_type']."_".$input['account_id'];

                $member_account_ids = array_unique($member_account_ids);
                $chat['title']=$account['nickname'];
                $chat['create_account_id']=$input['account_id'];
                $chat['main_account_id']=$input['account_id'];
                $chat['create_account_type']=$input['account_type'];
                $chat['main_account_type']=$input['account_type'];
                $chat['logo']=$account['logo'];
                $chat['chat_type']="service";
                $chat['member_account_ids']=implode(",",$member_account_ids);
                $chat['token']=ROOT_TOKEN;

                $chat=get_curl('mqtt',"msg_server/create_chat",$chat);
//                halt($chat);
                if($chat["status"]){
                    $chat=$chat['data'];
                }else{
                    return $this->errorJson($chat);
                }
                //添加客服窗口

                $service_chat['chat_id']=$chat['id'];
                $service_chat['is_close']=true;
//                $service_chat['in_service']=false;
                $service_chat['logo']=$account['logo'];
                $service_chat['service_title']=$chat['title'];
                $service_chat['create_time']=time();
                $service_chat['merchant_id']=$input['merchant_id'];
                $service_chat['merchant_type']=$input['merchant_type'];
                $service_chat['user_identity']=$input['user_identity'];

                $service_chat['user_id']= $account['account_id'];
                $service_chat['user_type']=$input['account_type'];

                $service_chat_id=Db::name('service_chat')->insertGetId($service_chat);
                $service_chat=Db::name('service_chat')->find($service_chat_id);

                //组织聊天参与人员
                $service_accounts=[];
                foreach ($chat['members'] as $chat_member){
                    //添加参与用户
                    if($chat_member['account_type']==$account['account_type']&&$chat_member['account_id']==$account['account_id']){
                        $service_user=[];
                        $service_user['chat_id']=$chat['id'];
                        $service_user['service_chat_id']=$service_chat['id'];
                        $service_user['create_time']=time();
                        $service_user['merchant_id']=$service_chat['merchant_id'];
                        $service_user['user_account_id']=$input['account_id'];
                        $service_user['user_account_type']=$input['account_type'];
                        $service_user['service_name']=$chat_member['nickname'];
                        $service_user['logo']=$chat_member['logo'];
                        $service_user['phone']=$chat_member['phone'];

                    }else{//参与客服
                        $service_account=[];
                        $service_account['chat_id']=$chat['id'];
                        $service_account['service_chat_id']=$service_chat['id'];
                        $service_account['merchant_id']=$service_chat['merchant_id'];
                        $service_account['service_account_id']=$chat_member['account_id'];
                        $service_account['service_account_type']=$chat_member['account_type'];
                        $service_account['service_name']=$chat_member['nickname'];
                        $service_account['logo']=$chat_member['logo'];
                        $service_account['phone']=$chat_member['phone'];
                        $service_accounts[]=$service_account;

                    }

                }
//                halt($service_user);
                //添加参与客服
                Db::name("service_account")->insertAll($service_accounts);

                //添加参与用户
                Db::name("service_user")->strict(false)->insertGetId($service_user);

            }
        }

        $service_accounts=Db::name("service_account")->where('service_chat_id',$service_chat['id'])->select();
        $service_users=Db::name("service_user")->where('service_chat_id',$service_chat['id'])->select();
//       dump($service_users);
        //待接入列表
        if($service_chat['is_close']){//TODO 判断上次是否已经结束,如果上次会话已经关闭，重新启动对话，并且通知商城所有客服，对话框在待接入状态
            $member_account_ids=[];
            //加上管理员
            foreach ($service_accounts as $service_account){
                $member_account_ids[]=$service_account['service_account_type']."_".$service_account['service_account_id'];
            }
            //加上用户
            foreach ($service_users as $service_user){
                $member_account_ids[]=$service_user['user_account_type']."_".$service_user['user_account_id'];
            }
            //TODO 通知商城所有客服

            $service_chat['service_accounts']=$service_accounts;
            $service_chat['service_users']=$service_users;

            $create_msg['token']=ROOT_TOKEN;

            $create_msg['chat_id']=0;
            $create_msg['send_account_id'] = 0;
            $create_msg['send_account_type']="a";
            $create_msg['msg_type']="create_chat";
            $create_msg['content']=json_encode($service_chat);
            $create_msg['accept_account_id']=implode(",",$member_account_ids);
            $msg=get_curl("mqtt","msg_server/send_msg",$create_msg);
//            logs('msg',$msg);

            //TODO 对话框在待接入状态，is_close状态改成在开启中
            Db::name('service_chat')->where('chat_id',$service_chat['chat_id'])->update(array('is_close'=>false));

        }
        $service_chat = Db::name('service_chat')->where('chat_id',$service_chat['chat_id'])->find();

        $service_chat['service_accounts']=$service_accounts;
        $service_chat['service_users']=$service_users;
//        $service_chat['service_dialog']=$service_dialog;

		//接口输出json数据
        $msg['data'] = $service_chat;
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}

    /**
     * @title 客服接入
     * @description 客服接入
     * @author 开发者（梁敏）
     * @url /service/service_server/in_service
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:chat_id type:Int require:1 default:1 other: desc:窗口id
    //     * @param name:send_account_id type:Int require:1 default:1 other: desc:帐号id
    //     * @param name:send_account_type type:String require:1 default:1 other: desc:帐号类型
     * @param name:service_account_id type:Int require:1 default:1 other: desc:帐号id
     * @param name:service_account_type type:String require:1 default:1 other: desc:帐号类型
     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
    public function in_service (){
        //模块 - 输入数据
        $data = input();
        $validate = new \think\Validate;
        $validate->rule([
            'chat_id'  => 'require|number',
            'service_account_id' => 'require|number',
            'service_account_type' => 'require'
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        //查询条件
        $temp=[
            'chat_id'=>$data['chat_id'],
            'service_account_id'=>$data['service_account_id'],
            'service_account_type'=>$data['service_account_type']
        ];

        //其他人不能接收
//        $res=Db::name('service_account')->where('chat_id',$data['chat_id'])->update(array('can_send'=>false,'in_accept'=>false));
        $res=Db::name('service_account')->where('chat_id',$data['chat_id'])->update(array('in_service'=>false,'is_send'=>false,'is_accept'=>false));

        //接入人可以接收
        $update_service_account['in_service']=1;
        $update_service_account['is_accept']=1;
        $update_service_account['is_send']=1;
        $res2=Db::name('service_account')->where($temp)->update($update_service_account);

        //用户进入待回复
        $update_service_user['in_service']=1;
        $update_service_user['is_accept']=1;
        $update_service_user['is_send']=1;
        $res3=Db::name('service_user')->where('chat_id',$data['chat_id'])->update($update_service_user);
        //TODO 同步聊天参与人员

        if($res2){
            $msg['msg']="接入成功";
            $service_accounts=Db::name('service_account')->where('chat_id',$data['chat_id'])->select();
            $service_users=Db::name('service_user')->where('chat_id',$data['chat_id'])->select();
            $member_account_ids=[];
            //加上管理员
            foreach ($service_accounts as $service_account){
                $member_account_ids[]=$service_account['service_account_type']."_".$service_account['service_account_id'];
            }
            //加上用户
            foreach ($service_users as $service_user){
                $member_account_ids[]=$service_user['user_account_type']."_".$service_user['user_account_id'];
            }
//            dump($online_chat_members);
            //该商城最后服务的客服ID

            Db::name('service_chat')->where('chat_id',$data['chat_id'])->update(array('last_service_account_id'=>$data['service_account_id'],'in_service'=>true,'last_service_account_type'=>$data['service_account_type']));

            $service_chat= Db::name('service_chat')->where('chat_id',$data['chat_id'])->find();

            $_msg=[];
            $_msg['token']=ROOT_TOKEN;
            $_msg['send_account_id']=0;
            $_msg['chat_id']=$data['chat_id'];
            $_msg['content']=json_encode($service_chat);
            $_msg['accept_account_id']=implode(",",$member_account_ids);
            $_msg['msg_type']='in_service';
            get_curl("mqtt","msg_server/send_msg",$_msg);
            return $this->successJson($msg);
        }else{
            $msg['msg']="接入失败";
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 发送窗口消息
     * @description 发送窗口消息
     * @author 开发者（梁敏）
     * @url /service/service_server/send_msg
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:chat_id type:Int require:1 default:1 other: desc:窗口id
     * @param name:send_account_id type:Int require:0 default:1 other: desc:帐号id
     * @param name:send_account_type type:Int require:0 default:1 other: desc:帐号类型
     * @param name:accept_account_id type:String require:0 default:0 other: desc:接收用户，类型_id，逗号隔开
     * @param name:content type:String require:0 default:0 other: desc:内容
     * @param name:msg_type type:String require:0 default:0 other: desc:普通文件，语音，视频，图片，链接
     * @param name:is_file type:int require:0 default:0 other: desc:是文件（0-否；1-是；）
     * @param name:file_name type:String require:0 default:0 other: desc:文件名
     * @param name:file_size type:String require:0 default:0 other: desc:文件大小
     * @param name:file_type type:String require:0 default:0 other: desc:文件类型（普通文件，语音，视频，图片）
     * @param name:file_url type:String require:0 default:0 other: desc:文件路径
     *
     *
     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
    public function send_msg (){
        //模块 - 输入数据
        $data = input();
        $validate = new \think\Validate;
        $validate->rule([
            'chat_id'  => 'number',
            'send_account_id' => 'number',
            'send_account_type'=>'require',
            'content' => 'require',
            'msg_type' => 'require'
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }
        //获取客服对话用户列表
        if(!$data['accept_account_id']){

//            //查询条件
            $temp=[
                'chat_id'=>$data['chat_id'],
                'in_service'=>true,
                'is_accept'=>true
            ];

            //获取客服对话
            $service_accounts = Db::name("service_account")->where($temp)->select();
            $user_accounts = Db::name("service_user")->where($temp)->select();
            $accept_account_ids=[];

            foreach ($service_accounts as $service_account){
                $accept_account_id=$service_account['service_account_type']."_".$service_account['service_account_id'];
                $accept_account_ids[]=$accept_account_id;
            }
            foreach ($user_accounts as $user_account){
                $accept_account_id=$user_account['user_account_type']."_".$user_account['user_account_id'];
                $accept_account_ids[]=$accept_account_id;
            }
            $data['accept_account_id']=implode(',',$accept_account_ids);

        }
//        logs("msg_server_send_msg",$data);
        $msg=get_curl("mqtt","msg_server/send_msg",$data);
        return $this->successJson($msg);
    }

    /**
     * @title 发送产品消息
     * @description 发送产品消息
     * @author 开发者（梁敏）
     * @url /service/service_server/send_product
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:chat_id type:Int require:1 default:1 other: desc:窗口id
     * @param name:send_account_id type:Int require:1 default:1 other: desc:帐号id
     * @param name:product_id type:Int require:1 default:0 other: desc:产品id
     * @param name:product_sku_id type:Int require:0 default:0 other: desc:产品sku_id
     * @param name:product_type type:String require:0 default:0 other: desc:产品类型
     *
     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
    public function send_product (){
        //模块 - 输入数据
        $data = input();
        $validate = new \think\Validate;
        $validate->rule([
            'chat_id'  => 'number',
            'send_account_id' => 'number',
            'product_id' => 'require',
            'product_type' => 'require'
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        $list=$this->get_goods_product($data['product_id'],$data['product_sku_id'],$data['product_type']);
//        echo "<pre>";
//        halt($list);

        $service_product = array();//定义空数组
        $service_product['merchant_id'] = $list['data']['merchant_id'];
        $service_product['img_url'] = $list['data']['img_url'];  //string(68)
        $service_product['product_url'] = $list['data']['product_url'];  //string(64)
        $service_product['product_info'] = json_encode($list['data']['product_info']);  //string(669)
//        echo "<pre>";
//        halt($service_product);

        //获取客服对话
        $service_chat=Db::name("service_chat")->where("chat_id",$data['chat_id'])->find();
        $service_product['chat_id']=$data['chat_id'];
        $service_product['service_chat_id']=$service_chat['id'];
        $service_product['create_time']=time();
        $service_product['product_id']=$data['product_id'];
        $service_product['product_type']=$data['product_type'];
        $service_product['product_sku_id']=$data['product_sku_id'];
//        echo "<pre>";
//        halt($service_product);

        //查看是否已经有资讯过该产品
        $service_product_id=Db::name("service_product")->insertGetId($service_product);
        $service_product=Db::name("service_product")->find($service_product_id);

        $service_accounts = Db::name("service_account")->where('chat_id',$data['chat_id'])->select();
        $user_accounts = Db::name("service_user")->where('chat_id',$data['chat_id'])->select();

        $accept_account_ids=[];

        foreach ($service_accounts as $service_account){
            $accept_account_id=$service_account['service_account_type']."_".$service_account['service_account_id'];
            $accept_account_ids[]=$accept_account_id;
        }
        foreach ($user_accounts as $user_account){
            $accept_account_id=$user_account['user_account_type']."_".$user_account['user_account_id'];
            $accept_account_ids[]=$accept_account_id;
        }

        $data['msg_type']="product";
        $data['content']=json_encode($service_product);
        $data['accept_account_id']=implode(',',$accept_account_ids);
        $msg=get_curl("mqtt","msg_server/send_msg",$data);
        return $this->successJson($msg);
    }

    /**
     * @title 发送订单消息
     * @description 发送订单消息
     * @author 开发者（梁敏）
     * @url /service/service_server/send_order
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:chat_id type:Int require:1 default:1 other: desc:窗口id
     * @param name:send_account_id type:Int require:1 default:1 other: desc:帐号id
     * @param name:send_account_type type:Int require:1 default:1 other: desc:帐号类型
     * @param name:order_id type:String require:1 default:0 other: desc:order_id
//     * @param name:order_type type:String require:0 default:0 other: desc:订单类型
     *
     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
    public function send_order (){
        //模块 - 输入数据
        $data = input();

        $validate = new \think\Validate;
        $validate->rule([
            'chat_id'  => 'number',
            'send_account_id' => 'number',
            'send_account_type' => 'require',
            'order_id' => 'require'
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

        $list=$this->get_order($data['order_id']);

        $service_order = array();//定义空数组
        //string(68)
        $service_order['img_url'] = $list['data']['img_url'];
        //string(55)
        $service_order['order_url'] = $list['data']['order_url'];
        //string(1558)
        $service_order['order_info'] = json_encode($list['data']['order_info']);

        //获取客服对话
        $service_chat=Db::name("service_chat")->where("chat_id",$data['chat_id'])->find();


        $service_order['chat_id']=$data['chat_id'];
        $service_order['service_chat_id']=$service_chat['id'];
        $service_order['create_time']=time();
        $service_order['order_id']=$data['order_id'];


        $service_order_id=Db::name("service_order")->insertGetId($service_order);
        $service_order=Db::name("service_order")->find($service_order_id);

        $service_accounts = Db::name("service_account")->where('chat_id',$data['chat_id'])->select();
        $user_accounts = Db::name("service_user")->where('chat_id',$data['chat_id'])->select();

        $accept_account_ids=[];

        foreach ($service_accounts as $service_account){
            $accept_account_id=$service_account['service_account_type']."_".$service_account['service_account_id'];
            $accept_account_ids[]=$accept_account_id;
        }
        foreach ($user_accounts as $user_account){
            $accept_account_id=$user_account['user_account_type']."_".$user_account['user_account_id'];
            $accept_account_ids[]=$accept_account_id;
        }

        $data['msg_type']="order";
        $data['content']=json_encode($service_order);
        $data['accept_account_id']=implode(',',$accept_account_ids);
        $msg=get_curl("mqtt","msg_server/send_msg",$data);
        return $this->successJson($msg);
    }

    /**
     * @title 获取产品信息
     * @description 获取产品信息
     * @author 开发者（梁敏）
     * @url /service/service_server/get_goods_product
     * @method *
     *
     * @param name:token require:1 default: desc:token
     * @param name:product_id type:Int require:1 default:0 other: desc:产品id
     * @param name:product_sku_id type:String require:0 default:0 other: desc:产品sku_id
     * @param name:product_type type:String require:1 default:0 other: desc:产品类型
     *
     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
    public function get_goods_product($product_id,$product_sku_id,$product_type)
    {
        $params['token'] = $this->token;
        $params['id'] = $product_id;
        $params['product_type'] = $product_type;
        $params['product_sku_id'] = $product_sku_id;

        switch ($params['product_type']){
            case 'good' :
                $url = $this->product_urls['goods'];
                break;
            case 'wholesale' :
                $url = $this->product_urls['wholesale'];
                break;
            case 'bargain' :
                $url = $this->product_urls['bargain'];
                break;
            case 'spike' :
                $url = $this->product_urls['spike'];
                break;
            default:
                $url = $this->product_urls['goods'];
        }

        $result = \tools\curl\Curl::get($url,$params);
//        echo "<pre>";
//        halt($result);


        if ($result==false) {
            $result['status'] = false;
            $result['msg'] = '请求失败1';
            $result['url'] = $url;
            $result['params'] = $params;
            return $this->errorJson($result);
        } else {
            $result = \tools\curl\Curl::json2arr($result);
            if ($result['status'] === false) {
                $result['data'] = '数据不存在';
                return $this->errorJson($result);
            }


             //产品链接内容
             $data['product_id'] = $result['data']['id'];
             $data['product_type'] = $product_type;
//            $data['title']=$result['data']['goodName'];
             $data['img_url'] = $result['data']['mainImg'];
//            $data['price']=$result['data']['sellingPrice'];
             $data['merchant_id'] = $result['data']['merchantID'];
             $data['product_url'] = $url . "/id/" . $product_id;
             $data['product_info'] = $result['data'];

             $arr = $data['product_info']['skus'];
             $sku = array();    // 定义新数组
             foreach ($arr as $key=>$val){
                if($val['id'] == $params['product_sku_id']){
                    $sku = $val;
                }
             }

             $data['product_info']['sku'] = $sku;
             unset($data['product_info']['skus']);



//             echo "<pre>";
//             halt($data);

             //返回内容
             $msg['status'] = true;
             $msg['msg'] = "获取成功";
             $msg['data'] = $data;

        }
//        return $this->successJson($msg);
        return $msg;
    }

    /**
     * @title 获取订单消息
     * @description 获取订单消息
     * @author 开发者（梁敏）
     * @url /service/service_server/get_order
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:order_id type:Int require:1 default:0 other: desc:order_id
     *
     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
    public function get_order($order_id)
    {
        $url = $this->order_url['order'];
        $params['token'] = $this->token;
        $params['id'] = $order_id;

        $result = \tools\curl\Curl::get($url, $params);

        if (!$result) {
            $result['status'] = false;
            $result['msg'] = '请求失败2';
            $result['url'] = $url;
            $result['params'] = $params;
            return $this->errorJson($result);
        } else {
            $result = \tools\curl\Curl::json2arr($result);

//            echo "<pre>";
//            halt($result);

        if ($result['status'] === false) {
           $result['data'] = '数据不存在';
           return $this->errorJson($result);
        }

        //产品链接内容
         $data['order_id']=$result['data']['order']['id'];
         $data['title']=$result['data']['order_item'][0]['goodName'];
         $data['img_url']=$result['data']['order']['goodImg'];
         $data['price']=$result['data']['order']['payPrice'];
         $data['order_url']=$url."/id/".$order_id;
         $data['order_info']=$result['data'];

//            echo "<pre>";
//            dump($data);

         //返回内容
         $msg['status']=true;
         $msg['msg']="获取成功";
         $msg['data']=$data;
//            echo "<pre>";
//            dump($msg);

        }


        //以数组的形式返回给send_order
        return $msg;
        $data=get_curl("mqtt","Msg/get_select",$msg);
//        return $this->successJson($msg);
    }

    /**
     * @title 关闭咨询窗口
     * @description 关闭咨询窗口
     * @author 开发者（梁敏）
     * @url /service/service_server/close_chat
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:account_id type:Int require:1 default:0 other: desc:帐号id
     * @param name:account_type type:String require:1 default:0 other: desc:帐号类型
     * @param name:merchant_id type:Int require:0 default:0 other: desc:商家id
     * @param name:merchant_type type:String require:0 default:0 other: desc:商家类型
     * @param name:chat_id type:Int require:0 default:0 other: desc:窗口id
     *
     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
    public function close_chat (){
        //模块 - 输入数据
        $input = input();
        //模块 - 验证器
        $validate = new \think\Validate;
        $validate->rule([
            'account_id'  => 'number',
            'merchant_id'  => 'number',
            'chat_id'  => 'number|require',
        ]);

        if (!$validate->check($input)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }

//        if(isset($input['chat_id'])){
//            $where['chat_id']=$input['chat_id'];
//        }

//        if(isset($input['merchant_id'])){
//            $where['merchant_id']=$input['merchant_id'];
//        }


        //获取对话框
        $where['chat_id'] = $input['chat_id'];

        Db::name('service_chat')->where($where)->update(['is_close' => 1,'close_time' => time()]);
        $service_chat = Db::name('service_chat')->where($where)->find();
//        halt($service_chat);


        //获取客服
//        Db::name('service_account')->where('service_chat_id',$service_chat['id'])->update(['in_service' => 0]);
        $service_accounts = Db::name("service_account")->where('service_chat_id',$service_chat['id'])->select();

//        $service_accounts[0]['in_service'] = 0;
//        halt($service_accounts);

//        $service_chat['last_service_account_id'] = $service_accounts[0]['service_account_id'];
//        $service_chat['last_service_account_type'] = $service_accounts[0]['service_account_type'];

        //获取用户
//        Db::name('service_user')->where('service_chat_id',$service_chat['id'])->update(['in_service' => 0]);
        $service_users=Db::name("service_user")->where('service_chat_id',$service_chat['id'])->select();
//        $service_users[0]['in_service'] = 0;
//        halt($service_users);

        //获取未关闭的对话
        $service_dialog=Db::name("service_dialog")
            ->where('service_chat_id',$service_chat['id'])
            ->where('is_close',false)
            ->find();
//        halt($service_dialog);
//        $service_chat['last_dialog_id'] = $service_dialog['id'];


        $service_chat['service_accounts'] = $service_accounts;
        $service_chat['service_users'] = $service_users;
        $service_chat['service_dialog'] = $service_dialog;


        $msg['data'] = $service_chat;
        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
}
